using System;
using System.Data;
using System.Configuration;
using System.Web.UI.WebControls;
using System.IO;
//using Microsoft.Health.Samples.PatientConnect.TreyResearch.HealthVaultAdapter;
public partial class ViewLabResults : System.Web.UI.UserControl
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            HiddenFieldSortExpression.Value = "Creation_Date_Time";
            HiddenFieldSortOrder.Value = "ASC";
            BindGridView();
        }
    }
    private void BindGridView()
    {
        if (!string.IsNullOrEmpty(ControlHelperClass.LabResultFolderPath))
        {
            DirectoryInfo dirInfo = new DirectoryInfo(ControlHelperClass.LabResultFolderPath);
            FileInfo[] fileInfo = dirInfo.GetFiles("*.xml", SearchOption.AllDirectories);
            DataTable LabResultTable = new DataTable();
            LabResultTable.Columns.Add("Creation_Date_Time");
            LabResultTable.Columns.Add("LabResult_Document");
            LabResultTable.Columns.Add("Upload");
            foreach (FileInfo finfo in fileInfo)
            {
                if (finfo.Name.Contains(ControlHelperClass.PatientId.ToString()))
                {
                    object[] parameter = new object[3];
                    parameter[0] = finfo.CreationTime;
                    parameter[1] = finfo.Name;
                    if (finfo.FullName.ToLower().Contains("uploaded"))
                    {
                        parameter[2] = "Y";
                    }
                    else
                    {
                        parameter[2] = "N";
                    }
                    LabResultTable.Rows.Add(parameter);
                }
            }
            if (LabResultTable.Rows.Count > 0)
            {
                LabResultTable.DefaultView.Sort = HiddenFieldSortExpression.Value + " " + HiddenFieldSortOrder.Value;
                GridViewLabResults.DataSource = LabResultTable.DefaultView;
                GridViewLabResults.DataBind();
                LabelNoRecords.Visible = false;
            }
            else
            {
                LabelNoRecords.Text = "No Lab results found.";
                LabelNoRecords.Visible = true;
            }
        }
    }
    public void Button1_Click(object o, EventArgs e)
    {
        LinkButton Button1 = (LinkButton)o;
        GridViewRow grdRow = (GridViewRow)Button1.Parent.Parent;
        LinkButton documentName = (LinkButton)grdRow.Cells[1].FindControl("LabelDocumentName");
        Label uploadedLable = (Label)grdRow.Cells[2].FindControl("LabelUploadFlag");
        DivLabResult.Visible = true;
        string labResultpath = string.Empty;
        string filename = documentName.Text;
        if (uploadedLable.Text.ToUpper().Equals("Y"))
        {
            labResultpath = Path.Combine(ControlHelperClass.LabResultFolderPath + @"\Uploaded", filename);
        }
        else
        {
            labResultpath = Path.Combine(ControlHelperClass.LabResultFolderPath, filename);
        }

        if (!string.IsNullOrEmpty(labResultpath))
        {
            using (StreamReader labResultStream = new StreamReader(labResultpath, true))
            {
                DivLabResult.InnerHtml = ControlHelperClass.HandleJunkChar(labResultStream.ReadToEnd());
            }
        }
    }
    protected void GridViewLabResults_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridViewLabResults.PageIndex = e.NewPageIndex;
        BindGridView();
    }
    protected void GridViewLabResults_Sorting(object sender, GridViewSortEventArgs e)
    {
        HiddenFieldSortExpression.Value = e.SortExpression;
        if (HiddenFieldSortOrder.Value != "ASC")
        {
            HiddenFieldSortOrder.Value = "ASC";
        }
        else
        {
            HiddenFieldSortOrder.Value = "DESC";
        }
        BindGridView();
    }
}